/**
* 头部菜单
*/ 
<template>
  <el-menu class="el-menu-demo" mode="horizontal" background-color="#334157" text-color="#fff" active-text-color="#fff">
    <el-button class="buttonimg">
      <img class="showimg" :src="collapsed?imgsq:imgshow" @click="toggle(collapsed)">
    </el-button>
    <span class="showimg">{{title}}</span>
    <el-submenu index="2" class="submenu">
      <!-- <template slot="title">{{user.userRealName}}</template> -->
      <template slot="title">{{user.nickName}}</template>
      <el-menu-item @click="content()" index="2-2">个人中心</el-menu-item>
      <el-menu-item @click="exit()" index="2-3">退出</el-menu-item>
    </el-submenu>
    <el-drawer
      size="30%"
      style="height: 550px;"
      title="修改个人信息"
      :visible.sync="drawer"
      direction="rtl">
      <el-form label-width="80px" :model="user">
        <el-form-item label="头像">
          <el-upload
            list-type="picture-card"
            action="/img/ngUploadImg"
            :file-list="imageList"
            :auto-upload="false"
            :on-change="importExcel"
            :on-remove="fileRemove"
            :limit="1"
            multiple
            accept=".jpg, .jpeg, .png"
            >    
          <i class="el-icon-plus"></i>             
          </el-upload>
        </el-form-item>
        <el-form-item label="账号" prop="userName">
          <el-input size="small" v-model="user.userName" auto-complete="off" placeholder="请输入账号"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input size="small" v-model="user.password" auto-complete="off" placeholder="请输入密码"></el-input>
        </el-form-item>
        <el-form-item label="昵称" prop="nickName">
          <el-input size="small" v-model="user.nickName" auto-complete="off" placeholder="请输入昵称"></el-input>
        </el-form-item>
        <el-form-item label="邮箱" prop="thirdParty">
          <el-input size="small" v-model="user.thirdParty" auto-complete="off" placeholder="请输入邮箱"></el-input>
        </el-form-item>
      </el-form>
      <div style="float: right;">
          <el-button size="small" @click='closeDialog()'>取消</el-button>
          <el-button size="small" type="primary" class="title" @click="update()">保存</el-button>
      </div>
    </el-drawer>
  </el-menu>
</template>
<script>
import { getLaboratoryInfo } from '../api/shiyanshi'
import { fileUpload } from '../api/file'
import { getFileNameFromPath } from '../utils/util'
import { updateUserInfo } from '../api/user'
export default {
  name: 'navcon',
  data() {
    return {
      title: sessionStorage.getItem("title"),
      collapsed: true,
      imgshow: require('../assets/img/show.png'),
      imgsq: require('../assets/img/sq.png'),
      user: {},
      title: "",
      drawer:false,
      imageList:[]
    }
  },
  // 创建完毕状态(里面是操作)
  created() {
    this.getDataInfo()
    this.user = JSON.parse(sessionStorage.getItem('sysuser'))
  },
  methods: {
    fileRemove(){
      this.user.icon = '';
    },
    importExcel(file){
      let formData = new FormData()
      formData.append('multipartFile', file.raw)
      fileUpload(formData).then(res =>{
        this.user.icon = res.data;
        this.user.imageUrl = getFileNameFromPath(res.data);
      })
    },
    update(){
      updateUserInfo(this.user).then(res =>{
        if(res.status == 200){
          sessionStorage.setItem('sysuser',this.user)
          this.$message({
            type: 'success',
            message: "修改成功"
          })
          this.drawer = false
        }
      })
    },
    closeDialog(){
      this.drawer = false
    },
    content(){
      this.drawer = true
    },
    getDataInfo(){
      let param = {};
      param.id = sessionStorage.getItem("mainId");
      getLaboratoryInfo(param).then(res =>{
        if(res.status == 200){
          this.title = res.data.name
        }
      });
    },
    // 退出登录
    exit() {
      this.$confirm('退出登录, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          setTimeout(() => {
            this.$store.commit('LOGIN_OUT')
            this.$router.push({ path: '/login' })
            this.$message({
              type: 'success',
              message: '已退出登录!'
            })
          }, 1000)
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消'
          })
        })
    },
    // 切换显示
    toggle(showtype) {
      this.collapsed = !showtype
      this.$root.Bus.$emit('toggle', this.collapsed)
    }
  }
}
</script>
<style scoped>
.el-menu-vertical-demo:not(.el-menu--collapse) {
  border: none;
}
.submenu {
  float: right;
}
.buttonimg {
  height: 60px;
  background-color: transparent;
  border: none;
}
.showimg {
  width: 26px;
  height: 26px;
  top: 17px;
  left: 17px;
  text-align: center;
  font-size: 20px;
  color: white;
}
.showimg:active {
  border: none;
}
</style>